package com.ts.proj.controller;

import com.ts.base.controller.BaseController;
import com.ts.proj.entity.CarEntity;
import com.ts.proj.entity.TransportunitEntity;
import com.ts.proj.service.CarService;
import com.ts.proj.service.TransportunitService;
import com.ts.util.JurisdictionUtil;
import com.ts.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

//说明 字符串：Test，作为替换字符串

@Controller
public class CarController extends BaseController {

    @Autowired
    private TransportunitService transportunitService;

    @Autowired
    private CarService carService;

    @RequestMapping("/car-add/{car}")
    public ModelAndView carAdd(@PathVariable("car") String car){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){

            PageData param = new PageData();
            param.put("deleteFlag", "N");
            param.put("carIdUnique", car);
            List<CarEntity> all = carService.findAll(param);
            if(all.size()==1){
                modelAndView.addObject("pre", all.get(0).getCarId().substring(0,1));
                modelAndView.addObject("last", all.get(0).getCarId().substring(1));
                modelAndView.addObject("carInfo", all.get(0));
            }else{
                modelAndView.addObject("pre", "-1");
                modelAndView.addObject("last", "");
                modelAndView.addObject("carInfo", new CarEntity());
            }
            List<TransportunitEntity> units = transportunitService.findAll();
            List<PageData> unitsList = new ArrayList<>();
            for(TransportunitEntity entity:units){
                PageData temp = new PageData();
                temp.put("value", entity.getTransportUnit()+"|"+entity.getTransportPY());
                temp.put("name", entity.getTransportName()+"|"+entity.getTransportUnit());
                unitsList.add(temp);
            }
            modelAndView.addObject("units", unitsList);
            viewName = "business/car/car-add";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

}